<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="菜单类型" prop="mtype">
      <el-input v-model="dataForm.mtype" placeholder="菜单类型"></el-input>
    </el-form-item>
    <el-form-item label="事件类型" prop="eventType">
      <el-input v-model="dataForm.eventType" placeholder="事件类型"></el-input>
    </el-form-item>
    <el-form-item label="菜单名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="菜单名称"></el-input>
    </el-form-item>
    <el-form-item label="菜单url" prop="url">
      <el-input v-model="dataForm.url" placeholder="菜单url"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="父级id" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="父级id"></el-input>
    </el-form-item>
    <el-form-item label="消息类型" prop="msgType">
      <el-input v-model="dataForm.msgType" placeholder="消息类型"></el-input>
    </el-form-item>
    <el-form-item label="消息id" prop="msgId">
      <el-input v-model="dataForm.msgId" placeholder="消息id"></el-input>
    </el-form-item>
    <el-form-item label="原始id" prop="account">
      <el-input v-model="dataForm.account" placeholder="原始id"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          mtype: '',
          eventType: '',
          name: '',
          url: '',
          sort: '',
          parentId: '',
          msgType: '',
          msgId: '',
          account: '',
          createTime: ''
        },
        dataRule: {
          mtype: [
            { required: true, message: '菜单类型不能为空', trigger: 'blur' }
          ],
          eventType: [
            { required: true, message: '事件类型不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '菜单名称不能为空', trigger: 'blur' }
          ],
          url: [
            { required: true, message: '菜单url不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '父级id不能为空', trigger: 'blur' }
          ],
          msgType: [
            { required: true, message: '消息类型不能为空', trigger: 'blur' }
          ],
          msgId: [
            { required: true, message: '消息id不能为空', trigger: 'blur' }
          ],
          account: [
            { required: true, message: '原始id不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/cms/accountmenu/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.mtype = data.data.mtype
                this.dataForm.eventType = data.data.eventType
                this.dataForm.name = data.data.name
                this.dataForm.url = data.data.url
                this.dataForm.sort = data.data.sort
                this.dataForm.parentId = data.data.parentId
                this.dataForm.msgType = data.data.msgType
                this.dataForm.msgId = data.data.msgId
                this.dataForm.account = data.data.account
                this.dataForm.createTime = data.data.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cms/accountmenu/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'mtype': this.dataForm.mtype,
                'eventType': this.dataForm.eventType,
                'name': this.dataForm.name,
                'url': this.dataForm.url,
                'sort': this.dataForm.sort,
                'parentId': this.dataForm.parentId,
                'msgType': this.dataForm.msgType,
                'msgId': this.dataForm.msgId,
                'account': this.dataForm.account,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
